package com.ruoyi.project.module.ny.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class TemplateTool {
    // MySQL连接参数
    private static final String url = "jdbc:mysql://localhost:3307/yh-hospitalevent?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
    private static final String user = "root";
    private static final String password = "root";

    @Autowired
    private RedisTemplate redisTemplate;

    // 将字体转换为驼峰形
    public static String underscoreToCamelCase(String underscoreString) {
        StringBuilder camelCaseString = new StringBuilder();
        boolean capitalizeNext = false;

        for (char c : underscoreString.toCharArray()) {
            if (c == '_') {
                capitalizeNext = true;
            } else {
                if (capitalizeNext) {
                    camelCaseString.append(Character.toUpperCase(c));
                    capitalizeNext = false;
                } else {
                    camelCaseString.append(c);
                }
            }
        }
        return camelCaseString.toString();
    }

//    public static Map<String, String> getColumnComments(String tableName) {
//        Map<String, String> map = new HashMap<>();
//
//        try (Connection conn = DriverManager.getConnection(url, user, password)) {
//            String sql = "SELECT column_name AS columnName, column_comment AS columnComment FROM information_schema.columns WHERE TABLE_SCHEMA = 'yh-hospitalevent' AND TABLE_NAME = 'he_report_event'";
////            System.out.println(sql);
//            try (Statement stmt = conn.createStatement();
//                 ResultSet rs = stmt.executeQuery(sql)) {
//                while (rs.next()) {
//                    String name = rs.getString("columnName");
//                    String comment = rs.getString("columnComment");
//                    map.put(TemplateTool.underscoreToCamelCase(name), comment);
//                }
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return map;
//    }
    // 从数据库中获取字典信息并存储到 Redis 中
    public void getDictionaryDataFromDB() {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            // 连接数据库
            conn = DriverManager.getConnection(url, user, password);

            // SQL 查询语句
            String sql = "SELECT dict_type, dict_label, dict_value FROM sys_dict_data";

            // 执行查询
            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();

            HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();

            // 遍历查询结果，逐个存储到 Redis 中
            while (rs.next()) {
                String type = reshape(rs.getString("dict_type"));
                String value = rs.getString("dict_label");
                String label = rs.getString("dict_value");

                // 将字典信息存储到 Redis Hash 中
                hashOperations.put(type, label, value);

                // 设置过期时间
                redisTemplate.expire(type, 200, TimeUnit.SECONDS);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接
            try {
                if (rs != null) {
                    rs.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //统一前缀格式，去掉_
    public static String reshape(String str) {
        String input = str;

        // 找到第一个下划线的索引
        int indexOfUnderscore = input.indexOf("_");
        if (indexOfUnderscore != -1) {
            // 截取从第一个下划线之后开始的子字符串
            input = input.substring(indexOfUnderscore + 1);
        } else {
            // 如果字符串中没有下划线，则不做任何处理
            System.out.println("No underscore found in the input string.");
        }
        return underscoreToCamelCase(input);
    }
}
